package com.moive.news.tqnewsapplication.ui.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

import com.moive.news.tqnewsapplication.R;
import com.moive.news.tqnewsapplication.base.BaseActivity;
import com.moive.news.tqnewsapplication.networks.BaseApi;
import com.moive.news.tqnewsapplication.okmanager.OkManagerCallback;
import com.moive.news.tqnewsapplication.utils.LogUtil;
import com.moive.news.tqnewsapplication.utils.PreferenceUtil;
import com.moive.news.tqnewsapplication.utils.ToastUtils;
import com.zhy.http.okhttp.OkHttpUtils;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;

/**
 * Created by Administrator on 2017/12/12.
 * 意见反馈
 */
public class FeedbackActivity extends BaseActivity {
    @BindView(R.id.feedback_back)
    ImageView feedbackBack;
    @BindView(R.id.feedback_edit)
    EditText feedbackEdit;
    @BindView(R.id.feedback_button)
    Button feedbackButton;
    private String yijian = "", token = "";
    private static final String TAG = "FeedbackActivity";

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_feedback);
        ButterKnife.bind(this);
        init();
    }

    private void init() {
        feedbackBack.setOnClickListener(v -> finish());
    }

    @OnClick(R.id.feedback_button)
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.feedback_button:
                sendfeedback();
                break;
        }
    }

    private void sendfeedback() {
        yijian = feedbackEdit.getText().toString().trim();
        token = PreferenceUtil.getString("token", "");
        if (TextUtils.isEmpty(yijian)) {
            ToastUtils.ShortToast("意见不能为空！");
            return;
        }
        if (TextUtils.isEmpty(token)) {
            ToastUtils.ShortToast("你还未登录，请登录后在提交你的宝贵意见！");
        }
        OkHttpUtils.post()
                .url(BaseApi.FEEDBACK)
                .addParams("content", yijian)
                .addParams("token", token)
                .build().execute(new OkManagerCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                super.onError(call, e, id);
                LogUtil.e(TAG, e.toString());
            }

            @Override
            public void onResponse(String response, int id) {
                super.onResponse(response, id);
                LogUtil.i(TAG, response.toString());
                try {
                    JSONObject jsonObject = new JSONObject(response);
                    if (jsonObject.optInt("code") == 0) {
                        ToastUtils.ShortToast("提交成功,感谢你对我们的支持！");
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }
}
